<?php

/* 
 $ 配置文件管理器 CLASS_CONFIG_FILE
 $ sample code:
   $config_object = new CLASS_CONFIG_FILE('./test.config.php');
   $config_object->save();
*/
class CLASS_CONFIG_FILE {
	private $config_file;  // 配置文件
	private $config_data;  // 写入的数据

	function __construct($f) {
		$this->config_file = $f;
		if(file_exists($f)) {
			$this->config_data = require($f);
		}

		if(!is_array($this->config_data)) {
		    $this->config_data = array();
		}
	}

	function CLASS_CONFIG_FILE($f) { $this->__construct($f); }

	function& config() { return $this->config_data;	}

	function save() {
	  $date = strftime("%Y-%m-%d %H:%M:%S", time());
	  $config = var_export($this->config_data, TRUE);
      $config_buffer = <<<STR
<?php
/*
 $ this file is generated by configure file tool.
 $ warnning: do not modify by manual.
 $ last update at {$date}. 
*/

return {$config};

?>
STR;
		
		// 写入配置文件
		$f = fopen($this->config_file, 'w+');
		if($f) {
			fwrite($f, $config_buffer, strlen($config_buffer));
			fclose($f);
		}
	}
}



?>